package com.nekonomics
package api.dto.response

import api.dto.util.SkuSpecUtil
import domain.detail.{CartItemDetail, SkuSpecDetail}
import domain.entity.Merchant
import types.*

import zio.json.JsonEncoder


private case class Item(
  skuId: Long,
  spuName: String,
  imageUrl: Option[String],
  price: Money,
  quantity: Int,
  specs: String
)derives JsonEncoder

object Item {
  def of(ci: CartItemDetail, specs: List[SkuSpecDetail]) = Item(
    skuId = ci.skuId,
    spuName = ci.spuName,
    imageUrl = ci.imageUrl,
    price = ci.price,
    quantity = ci.quantity,
    specs = SkuSpecUtil.fmtSpecs(specs)
  )
}

private case class CartItemsByMerchant(
  merchantId: Long,
  merchantName: String,
  merchantLogo: Option[String],
  items: List[Item]
)derives JsonEncoder

object CartItemsByMerchant {
  def of(merchant: Merchant, cartItems: List[(CartItemDetail, List[SkuSpecDetail])]): CartItemsByMerchant = {
    CartItemsByMerchant(
      merchantId = merchant.id,
      merchantName = merchant.name,
      merchantLogo = merchant.logo,
      items = cartItems.map { case (ci, specs) => Item.of(ci, specs) }
    )
  }
}


case class OrderConfirmDto(
  groups: List[CartItemsByMerchant],
  addresses: List[AddressDto],
  totalPrice: Money
)derives JsonEncoder
